


from __future__ import with_statement # needed for python 2.5
import os
from fabric.api import *
from fabric.contrib import files, console

def clone(cmd=None, url=None, target=None, user=None):
    '''
    clone:hg:'repo_url':'uc_site'
    '''
    space = ' '
    if cmd == None:
        cmd = 'hg'
    action = 'clone'
    if url == None:
        url = 'https://uc-site.googlecode.com/hg/'
    if target == None:
        target = 'uc_site'
    if user == None:
        print('no user specified for clone defaulting to None')
    env.command_line = cmd + space + action + space + url + space + target
    if not files.exists(os.path.join(env.w2p_apps, target)):
        with cd(env.w2p_apps):
            sudo(env.command_line, user=user)
    else:
        print('local clone already installed')

def pull(cmd=None, target=None, user=None):
    '''
    pull:cmd=hg:target='uc_site'
    '''
    space = ' '
    if cmd == None:
        cmd = 'hg'
    action = 'pull'
    if target == None:
        target = 'uc_site'
    if user == None:
        print('no user specified for clone defaulting to None')
    env.command_line = cmd + space + action
    repo = os.path.join(env.w2p_apps, target)
    if files.exists(repo):
        with cd(repo):
            sudo(env.command_line, user=user)
    else:
        print('project not found, has it been cloned?')

def update(cmd=None, target=None, user=None):
    '''
    update:hg:'uc_site'
    '''
    space = ' '
    if cmd == None:
        cmd = 'hg'
    action = 'update'
    if target == None:
        target = 'uc_site'
    if user == None:
        print('no user specified for clone defaulting to None')
    env.command_line = cmd + space + action
    repo = os.path.join(env.w2p_apps, target)
    if files.exists(repo):
        with cd(repo):
            sudo(env.command_line, user=user)
    else:
        print('project not found, has it been cloned?')

def push(cmd=None, target=None, user=None):
    '''
    push:hg:'https://uc-site.googlecode.com/hg/':'uc_site'
    '''
    space = ' '
    if cmd == None:
        cmd = 'hg'
    action = 'push'
    if target == None:
        target = 'uc_site'
    if user == None:
        print('no user specified for clone defaulting to None')
    env.command_line = cmd + space + action
    repo = os.path.join(env.w2p_apps, target)
    if files.exists(repo):
        with cd(repo):
            sudo(env.command_line, user=user)
    else:
        print('project not found, has it been cloned?')
